package com.example.a10928.weifriend.ui.login

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.util.Log
import android.widget.Toast
import com.example.a10928.weifriend.App
import com.example.a10928.weifriend.R
import com.example.a10928.weifriend.http.api.VariableName
import com.example.a10928.weifriend.http.configuration.ApiException
import com.example.a10928.weifriend.ui.BaseActivity
import com.example.a10928.weifriend.util.RxUtil
import com.jkb.vcedittext.VerificationAction
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_code.*
import kotlinx.android.synthetic.main.activity_phone_register.*

class CodeActivity : BaseActivity() {

    var handler:Handler?=null
    var time:Int=300

    override fun getContentView(): Int {
        return R.layout.activity_code
    }

    override fun initView() {

        supportActionBar?.hide()
    }

    override fun initData() {

        handler=object : Handler() {
            override fun handleMessage(msg: Message?) {
                super.handleMessage(msg)

                time=time-1
                if(time>=0){
                    tv_surplus.setText(time.toString()+"秒后重新发送")
                    handler?.sendEmptyMessageDelayed(0,1000)
                }
            }
        }

        App.app().bpService.sendCodeSMS(intent.getStringExtra(VariableName.PHONE))
                .compose(RxUtil.io2mainLoad(this))
                .subscribe(
                        Consumer {
                            handler?.sendEmptyMessageDelayed(0,1000)
                        },
                        Consumer {

                            RxUtil.errorException(it)
                        }
                )

        et_code.setOnVerificationCodeChangedListener(object :VerificationAction.OnVerificationCodeChangedListener{
            override fun onVerCodeChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

            override fun onInputCompleted(s: CharSequence?) {



                App.app().bpService.checkCode(intent.getStringExtra(VariableName.PHONE),s.toString())
                        .compose(RxUtil.io2mainLoad(this@CodeActivity))
                        .subscribe(
                                Consumer {

                                    var myIntent= Intent(this@CodeActivity,SetPWActivity::class.java)
                                    myIntent.putExtra(VariableName.PHONE,intent.getStringExtra(VariableName.PHONE))
                                    myIntent.putExtra(VariableName.CODE,s.toString())
                                    startActivity(myIntent)

                                    finish()
                                    Log.v("zzw","onSucess")

                                },
                                Consumer {
                                    RxUtil.errorException(it)
                                }
                        )






            }

        })

    }


}
